<?php

require __DIR__ . '/../vendor/autoload.php';

$app = require_once __DIR__ . '/../bootstrap/app.php';

$app->make(\App\Console\Kernel::class)->bootstrap();

/* ============== Write Codes Below ================= */

datamove();

function OtoArray($data)
{
    return json_decode(json_encode($data), true);
}

function datamove()
{
    $users = DB::table('dsc_users')->where('user_id', '>=', 251)
        ->get();
    $users = OtoArray($users);
    foreach ($users as $k => $v) {
        if (empty($v['qianbao'])) {
            continue;
        }

        $status = 'enabled';

        $user = DB::table('op_frontend_users')->where('mobile', $v['mobile_phone'])->first();
        $user = OtoArray($user);
        if (empty($user)) {
//            if($v['service_id'] == 0) {
//                $service = 3;
//            } else if($v['service_id'] == 1) {
            $service = 4;
//            }

            $parent_id = 0;
            if (0 != $v['parent_id']) {
                $parent = DB::table('dsc_users')->where('user_id', $v['parent_id'])->first();
                $parent = OtoArray($parent);

                $parent_id = DB::table('op_frontend_users')->where('mobile', $parent['mobile_phone'])->value('id');

                if (empty($parent_id)) {
                    $parent_id = 0;
                }
            }

            $data = [
                'created_at' => date('Y-m-d H:i:s', $v['reg_time']),
                'updated_at' => date('Y-m-d H:i:s', $v['reg_time']),
                'mobile' => $v['mobile_phone'],
                'nickname' => $v['nick_name'],
                'avatar_url' => '',
                'password' => '###',
                'wallet_address' => $v['qianbao'],
                'last_login_time' => date('Y-m-d H:i:s', $v['last_login']),
                'agent_id' => $service,
                'parent_id' => $parent_id,
                'status' => $status,
            ];

            $id = DB::table('op_frontend_users')->insertGetId($data);

            $user_real = DB::table('dsc_users_real')->where('user_id', $v['user_id'])->first();
            $user_real = OtoArray($user_real);

            if (empty($user_real)) {
                continue;
            }

            $province = DB::table('dsc_region')->where('region_id', $user_real['province'])->value('yb_code');
            $city = DB::table('dsc_region')->where('region_id', $user_real['city'])->value('yb_code');
            $county = DB::table('dsc_region')->where('region_id', $user_real['district'])->value('yb_code');

            if (empty($province) || empty($city) || empty($county)) {
                continue;
            }

            if (1 == $user_real['review_status']) {
                $audit_status = 'pass';
            } elseif (2 == $user_real['review_status']) {
                $audit_status = 'reject';
            } else {
                $audit_status = '';
            }

            $auth_data = [
                'created_at' => date('Y-m-d H:i:s', $user_real['add_time']),
                'updated_at' => date('Y-m-d H:i:s', $user_real['add_time']),
                'user_id' => $id,
                'real_name' => $user_real['real_name'],
                'id_card' => $user_real['self_num'],
                'id_card_front_image' => 'https://ban-dengji.obs.cn-north-4.myhuaweicloud.com/old_id_card/' . explode('/', $user_real['front_of_id_card'])[2],
                'id_card_back_image' => 'https://ban-dengji.obs.cn-north-4.myhuaweicloud.com/old_id_card/' . explode('/', $user_real['reverse_of_id_card'])[2],
                'province' => $province,
                'city' => $city,
                'district' => $county,
                'address' => $user_real['address'],
                'bank_code' => $user_real['bank_name'],
                'bank_card_no' => $user_real['bank_card'],
                'bank_card_mobile' => $user_real['bank_mobile'],
                'audit_status' => $audit_status,
                'yeepay_request_no' => $user_real['request_no'],
                'merchant_no' => $user_real['merchantNo'],
                'reject_reason' => $user_real['review_content'],
            ];

            DB::table('op_user_auths')->insert($auth_data);
        } else {
            $id = $user['id'];
        }

//        $sign = md5($this->apikey . $v['qianbao'] . $this->apisecret);
//
//
//        $walletUrl = $this->apiurl . '/sc/assetsList?walletAddress=' . $v['qianbao'] . '&appkey=' . $this->apikey . '&sign=' . $sign;
//
//        $result = $this->doGet($walletUrl);
//
//        $result = json_decode($result, true);

        /** @var \App\Services\WalletService $walletService */
        $walletService = app()->get(\App\Services\WalletService::class);

        $resu = $walletService->assetList($v['qianbao']);
        echo json_encode($resu);
//        if ($result['code'] == 200) {
//            $resu = $result['content'];
        if (count(array_column($resu, 'tokenIds')) > 50) {
            continue;
        }
        foreach ($resu as $kk => $vv) {
            foreach ($vv['tokenIds'] as $kkk => $vvv) {
                $token = DB::table('op_product_tokens')->where('token_id', $vvv['tokenId'])->first();
                $token = OtoArray($token);
                if (!empty($token) && 'init' == $token['status']) {
                    DB::table('op_product_tokens')->where('token_id', $vvv['tokenId'])->update([
                        'status' => 'sold',
                    ]);

                    DB::table('op_assets')->insert([
                        'user_id' => $id,
                        'source_type' => 'order',
                        'source_id' => 0,
                        'contract_address' => $vv['commodity'],
                        'token_id' => $vvv['tokenId'],
                        'trans_hash' => '',
                        'asset_status' => 'hold',
                    ]);
                }
            }
//            }
        }

        echo 1;
    }
}
